<?php

/**
 * 格子用户中心管理控制器
 *  
 */
class CatadAction extends UserAction {

    public function _initialize() {
        parent::_initialize();
        $userType = session('user_type');
        if ($userType != 'shop') {
            $this->error('操作有误：商户会员才可操作');
        }
    }

    public function index() {
        $this->display();
    }

    public function fabu() {
        if (IS_POST) {
            //校验敏感词
            $checkTitle            = D('Badword')->checkBadword($_POST['title']);
            if ($checkTitle['status'] == false)
                $this->error('标题包含敏感词 -- ' . $checkTitle['data']);
            //图片上传
            $savePath              = "/Public/Uploads/" . date("Ymd") . "/";
            $file                  = $this->upload($savePath);
            $_POST['main_img_url'] = $savePath . $file['main_img_url']['savename'];
            $_POST['pay_score']    = D('UserAccount')->countSum($_POST);
            $fabu                  = D('Catad')->fabu($_POST);
            if ($fabu['status'] == true) {
                $this->success('分类广告发布成功');
            } else {
                $this->error('分类广告发布失败' . $fabu['msg']);
            }
            exit;
        } else {
            //地区列表
            $areas = M('Area')->where(array('status' => 1))->select();
            //分类
            $cat     = D('Cat')->field("*,cat_id as id,parent_id as parentid")->where(array('parent_id' => 0))->order(array("sort_order" => "ASC"))->select();
            $this->assign('area', $areas);
            $this->assign('cat', $cat);
            $this->display();
        }
    }

    public function childCat() {
        $pid    = (int) $_REQUEST['pid'];
        $result = D('Cat')->field("name,cat_id as id,parent_id as parentid")->order(array("sort_order" => "ASC"))->select();
        import("Tree");
        $tree        = new Tree();
        $tree->init($result);
        $catArr      = $tree->get_tree_array($pid, '');
        $newArr      = array();
        foreach ($catArr as $cval) {
            $newArr[] = $cval;
        }
        $this->ajaxReturn(reStatus(true, '', $newArr));
    }

    public function adlist() {
        $uid   = session('uid');
        $model = M('Catad');
        $type  = t($_REQUEST['type']);

        $map = array();
        $map['user_id'] = $uid;
        if ($type == 'outoftime') {
            $map['end_time'] = array('elt', time());
        }

        //取得满足条件的记录数
        $count = $model
                ->where($map)
                ->count();
        if ($count > 0) {
            import("ORG.Util.Page");

            $limit = 8;
            $order = 'create_time DESC';
            //创建分页对象
            $p     = new Page($count, $limit);
            //分页查询数据

            $voList = $model
                    ->where($map)
                    ->order($order)
                    ->limit($p->firstRow . ',' . $p->listRows)
                    ->select();
            //分页模版
            $TP     = '共有{recordcount}{prev}&nbsp;{liststart}{list}&nbsp;{next}';
            $p->SetPager('default', $TP, array("listlong"      => "6", "first"         => "首页", "last"          => "尾页", "prev"          => "上一页", "next"          => "下一页", "list"          => "*", "disabledclass" => ""));

            //分页显示
            $page = $p->show();
            //模板赋值显示
            $data = array(
                'list'      => $voList,
                'page'      => $page,
                'nowPage'   => $_REQUEST['page'],
                'totalPage' => round($count / $limit),
            );
        }

        //推荐信息
        $roundDZ = _roundDazhe();
        $this->assign('rounddz', $roundDZ);

        $this->assign('data', $data);
        $this->display();
    }

}

?>
